/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI;
import javax.microedition.lcdui.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.rms.*;

/**
 *
 * @author Pisarchik_S
 */


public class AskForThanks implements ItemCommandListener {
    Dispatcher m_Dispatcher;


    Command m_DoneCommand = new Command("Далей", Command.ITEM, 0);
    Command m_CancelCommand = new Command("Далей", Command.ITEM, 0);
    Command m_NextTimeCommand = new Command("Далей", Command.ITEM, 0);

    public AskForThanks(Dispatcher dispatcher)
    {
        m_Dispatcher = dispatcher;
    }

    boolean m_bNeedShow = true;
    int m_Counter = 0;

    String m_saveName = "AskForThanks";

    private void Load()
    {
        try
        {
            RecordStore rs = RecordStore.openRecordStore(m_saveName, true);
            RecordEnumeration re = rs.enumerateRecords(null, null, false);
            if (re.numRecords() > 0)
            {
                byte[] arr = re.nextRecord();
                ByteArrayInputStream inputStream = new ByteArrayInputStream(arr);
                DataInputStream inputDataStream = new DataInputStream(inputStream);

                m_Counter = inputDataStream.readInt();
                m_bNeedShow = inputDataStream.readBoolean();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    private void Save()
    {
        try
        {
            RecordStore rs = RecordStore.openRecordStore(m_saveName, false);
            RecordEnumeration re = rs.enumerateRecords(null, null, false);
            while (re.hasNextElement())
            {
                rs.deleteRecord(re.nextRecordId());
            }

            ByteArrayOutputStream oStream = new ByteArrayOutputStream();
            DataOutputStream oDataStream = new DataOutputStream(oStream);
            oDataStream.writeInt(m_Counter);
            oDataStream.writeBoolean(m_bNeedShow);
            byte [] data = oStream.toByteArray();
            rs.addRecord(data, 0, data.length);

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

    }

    public void Init()
    {
        Load();
        m_Counter++;
        Save();

        if (m_bNeedShow == false || m_Counter < 7)
        {
            m_Dispatcher.GetShowStatistics().Init();
            return;
        }

        Form form = new Form("Зварот да карыстальніка");
        StringItem si = new StringItem("",
                "Паважаны карыстальнік. \n\n"
                + "Калі табе спадабалася гэтая праграма, то ты можаш "
                + "аддзячыць распрацоўшчыку.\n\n Для гэтага можна:\n"
                + "- або пакласці грошы на тэлефон +375 29 6089941, пажадана 0.5 даляра\n"
                + "- або падзяліцца праграмай са сваімі сябрамі і знаёмымі.\n\n"
                + "З павагай, Сяргей Пісарчык.\n\n");
        form.append(si);
        si = new StringItem("","Так, я ўжо аддзячыў");
        si.setDefaultCommand(m_DoneCommand);
        si.setItemCommandListener(this);
        si.setPreferredSize(400, 20);
        form.append(si);

        si = new StringItem("","Нагадайць мне наступным разам");
        si.setDefaultCommand(m_NextTimeCommand);
        si.setItemCommandListener(this);
        si.setPreferredSize(400, 30);
        form.append(si);

        si = new StringItem("","Не жадаю аддзячыць - не нагадваць мне больш ніколі");
        si.setDefaultCommand(m_CancelCommand);
        si.setItemCommandListener(this);
        si.setPreferredSize(400, 50);
        form.append(si);

        m_Dispatcher.GetDisplay().setCurrent(form);
    }

    public void commandAction(Command c, Item item)
    {
        if (c == m_DoneCommand)
        {
            m_bNeedShow = false;
        }
        if (c == m_CancelCommand)
        {
            m_bNeedShow = false;
        }
        Save();
        m_Dispatcher.GetShowStatistics().Init();
    }

}
